package com.cloud.freeflow.api.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cloud.freeflow.api.entity.ProcActDef;
import com.cloud.freeflow.api.entity.ProcTransDef;
import com.cloud.freeflow.api.mapper.ProcActDefMapper;
import com.cloud.freeflow.api.mapper.ProcTransDefMapper;
import com.cloud.freeflow.api.service.ProcActDefService;

import tk.mybatis.mapper.entity.Example;

@Service
@Transactional
public class ProcActDefServiceImpl implements ProcActDefService {

	@Autowired
	private ProcActDefMapper procActDefMapper;

	@Autowired
	private ProcTransDefMapper procTransDefMapper;

	@Override
	public List<ProcActDef> listProcActs(long id) {
		Example example = new Example(ProcActDef.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("procDefId", id);
		criteria.andEqualTo("deleteFlag", 0);
		return procActDefMapper.selectByExample(example);
	}

	@Override
	public ProcActDef createProcAct(ProcActDef act) {
		act.setDeleteFlag(0);
		procActDefMapper.insert(act);
		return act;
	}

	@Override
	public long deleteProcAct(long id) {
		ProcActDef act = new ProcActDef();
		act.setActDefId(id);
		act.setDeleteFlag(1);
		procActDefMapper.updateByPrimaryKeySelective(act);

		Example example = new Example(ProcTransDef.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("fromAct", id).orEqualTo("toAct", id);
		ProcTransDef trans = new ProcTransDef();
		trans.setDeleteFlag(1);
		procTransDefMapper.updateByExampleSelective(trans, example);
		return id;
	}

	@Override
	public long moveProcActs(ProcActDef act) {
		ProcActDef temp = new ProcActDef();
		temp.setPozLeft(act.getPozLeft());
		temp.setPozTop(act.getPozTop());
		temp.setActDefId(act.getActDefId());
		return procActDefMapper.updateByPrimaryKeySelective(temp);
	}

	@Override
	public ProcActDef loadByActDefId(Long actDefId) {
		return procActDefMapper.selectByPrimaryKey(actDefId);
	}

	@Override
	public void saveUpdateActDef(ProcActDef actDef) {
		ProcActDef act = procActDefMapper.selectByPrimaryKey(actDef.getActDefId());
		act.setActDefName(actDef.getActDefName());
		act.setLimitTime(actDef.getLimitTime());
		act.setWarnTime(actDef.getWarnTime());
		act.setActPropertyId(actDef.getActPropertyId());
		act.setActPropertyName(actDef.getActPropertyName());
		act.setActTypeId(actDef.getActTypeId());
		act.setActTypeName(actDef.getActTypeName());
		act.setActDefAttrId(actDef.getActDefAttrId());
		procActDefMapper.updateByPrimaryKey(act);
	}

}
